package com.yuchen.learning.controller;

import com.yuchen.learning.common.Result;
import com.yuchen.learning.entity.Note;
import com.yuchen.learning.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/note")
public class NoteController {
    @Autowired
    private NoteService noteService;

    // 新增笔记
    @PostMapping("/add")
    public Result<?> add(@RequestBody Note note) {
        boolean ok = noteService.save(note);
        return ok ? Result.success(null) : Result.error("添加失败");
    }

    // 查询某路线下所有笔记
    @GetMapping("/list")
    public Result<List<Note>> list(@RequestParam Long routeId) {
        List<Note> notes = noteService.lambdaQuery().eq(Note::getRouteId, routeId).list();
        return Result.success(notes);
    }

    // 更新笔记
    @PutMapping("/update")
    public Result<?> update(@RequestBody Note note) {
        boolean ok = noteService.updateById(note);
        return ok ? Result.success(null) : Result.error("更新失败");
    }

    // 删除笔记
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        boolean ok = noteService.removeById(id);
        return ok ? Result.success(null) : Result.error("删除失败");
    }
}